package com.aistar.controller;

import com.aistar.pojo.Customer;
import com.aistar.rsa_aes.AESCipher;
import com.aistar.rsa_aes.Config;
import com.aistar.rsa_aes.RSACipher;
import com.aistar.rsa_aes.SecureRandomUtil;
import com.aistar.service.CustomerService;
import com.aistar.util.GetMessageCode;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

@Controller
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @GetMapping("/customer/{id}")
    @ResponseBody
    public ServerResponse getById(@PathVariable("id")Integer custId){

        System.out.println(custId);
      ServerResponse serverResponse =  customerService.getById(custId);
      return serverResponse;
    }
    @RequestMapping("/order")
    public String order(){
        return "order/order";
    }



    /*@RequestMapping("/login")
    public String login(){
        return "customer/login";
    }
    @RequestMapping("/index")
    public String index(){
        return "index";
    }
    @RequestMapping("/register")
    public String register(){
        return "customer/register";
    }*/
    @GetMapping("/customer/getByTel")
    @ResponseBody
    public ServerResponse getByTel(String tel){
        System.out.println("tel"+tel);
        ServerResponse serverResponse = null;
        if (tel.equals("")) {
           serverResponse = ServerResponse.registerSuccess("请输入手机号");

        } else {
            serverResponse = customerService.getByTel(tel);
        }
        return serverResponse;
    }

    @GetMapping("/customer/getCode")
    @ResponseBody
    public ServerResponse getCode(String tel){
        System.out.println(tel);
        ServerResponse serverResponse = null;
        if (tel.equals("")) {
               serverResponse = ServerResponse.registerSuccess("请输入手机号");
               return serverResponse;
        } else {
            //手机号不为空，在判断手机号是否注册
            serverResponse = customerService.getByTel(tel);

            if (serverResponse.getStatus() == 0){       //手机号没有注册过，通过接口给验证码
                String code = GetMessageCode.getCode(tel);
                System.out.println("code:" + code);
                //将验证码通过data传进前端
                serverResponse = ServerResponse.registerFailed(code);
            }
        } //手机号已经注册过，提示手机号已经注册过
        return serverResponse;
    }




    @PostMapping("/customer/add")
    @ResponseBody
    public ServerResponse add(String custTelno, String custPassWord,String custName) throws Exception {
        //封装成对象
        Customer customer = new Customer();
        customer.setCustomerName(custName);
        customer.setCustomerTelno(custTelno);
        customer.setCustomerPassword(custPassWord);
        customer.setStatus("1");
        customer.setRegistDate(new Date());

        //添加到数据库
        ServerResponse serverResponse = customerService.add(customer);
        return serverResponse;
    }

    @PostMapping("/customer/login")
    @ResponseBody
    public ServerResponse login(String custAccount, String custPassWord, String type, HttpSession session) throws Exception {
        System.out.println("普通登录");
        ServerResponse serverResponse = customerService.getByAccountAndPw(type,custAccount,custPassWord);
//        System.out.println("serveresponse" + serverResponse.getData(););
        if(serverResponse.getStatus()==1){
            session.setAttribute("loginCustomer",serverResponse.getData());//绑定用户信息
            Customer customer = (Customer) session.getAttribute("loginCustomer");

        }
        return serverResponse;
    }
    @PostMapping("/customer/autoLogin")
    @ResponseBody
    public ServerResponse autoLogin(String custAccount, String custPassWord, String type, HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("自动登录");
        ServerResponse serverResponse = customerService.getByAccountAndPw(type,custAccount,custPassWord);
        if(serverResponse.getStatus()==1){
            Cookie cookie =  new Cookie("customer",custAccount + "&" + custPassWord);
            cookie.setMaxAge(7*24*60*60);
            cookie.setPath(request.getContextPath());
            response.addCookie(cookie);
            HttpSession session = request.getSession();
            session.setAttribute("loginCustomer",serverResponse.getData());//绑定用户信息
        }
        return serverResponse;
    }

    @RequestMapping("/customer/out")
    public void out(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {
        //1. 从session中移除登录用户
        session.removeAttribute("loginCustomer");
        session.removeAttribute("autoLoginName");
        //2. 从cookie中移除自动登录cookie
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie:cookies){
            if(cookie.getName().equals("customer")){

                cookie.setMaxAge(0);
                cookie.setPath(request.getContextPath());
                response.addCookie(cookie);

            }
        }
        //3. 跳转到登录
        response.sendRedirect(request.getContextPath() + "/customer/login.jsp");
    }
}
